﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ETADTools.Lib
{
  /// <summary>
  /// Builder to build Export Interface script
  /// </summary>
  public class ExportInterfaceBuilder
  {
    public FieldList FieldList { get; set; }
    public string AuthorName { get; set; }
    public string CreationDate { get; set; }
    public string InterfaceName { get; set; }

    /// <summary>
    /// Constructor for ExportScriptBuilder
    /// </summary>
    public ExportInterfaceBuilder(string interfaceName, FieldList fieldList, string author)
    {
      InterfaceName = interfaceName;
      FieldList = fieldList;
      AuthorName = author;
      CreationDate = DateTime.Today.ToString("dd-MMM-yyyy"); // get creation date
    }

    /// <summary>
    /// Build script
    /// </summary>
    /// <param name="updaterPath"></param>
    /// <returns></returns>
    public string Build()
    {
      StringBuilder result = new StringBuilder();

      result.AppendLine(BuildSqlExport());

      return result.ToString();
    }


    /// <summary>
    /// Build SQL script to export files
    /// </summary>
    /// <returns></returns>
    private string BuildSqlExport()
    {
      StringBuilder sql = new StringBuilder(Resource.EXPORT_INTERFACENAME_Execute);

      sql.Replace(PlaceHolder.AuthorName, AuthorName);
      sql.Replace(PlaceHolder.CreationDate, CreationDate);
      sql.Replace(PlaceHolderExportInterface.ExportInterfaceName, InterfaceName);

      sql.Replace(PlaceHolderExportInterface.ExportDeclareVariable, FieldList.ToSqlScript(",\n    ", Style.TypeAndSize, Style.Declaration));
      sql.Replace(PlaceHolderExportInterface.ExportCreateTable, FieldList.ToSqlScript(",\n    ", Style.TypeAndSize));
      sql.Replace(PlaceHolderExportInterface.ExportInsertInto, FieldList.ToSqlScript(",\n    "));
      sql.Replace(PlaceHolderExportInterface.ExportHeading, FieldList.ToSqlScript("|"));
      sql.Replace(PlaceHolderExportInterface.ExportSelect, FieldList.ToSqlExportSelect());

      sql.Replace(PlaceHolderExportInterface.ExportFetchNext, FieldList.ToSqlScript(",\n    ", Style.Declaration));

      sql.Replace(PlaceHolderExportInterface.ExportRowContent, FieldList.ToSqlExportRowContent());

      return sql.ToString();
    }
  }
}